package com.superhelp.ebill.util;


import com.superhelp.ebill.R;
import com.superhelp.ebill.model.TimeInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;


public class DateUtils {
	private static final long INTERVAL_IN_MILLISECONDS = 30000L;

	public static String getTimestampString(Date paramDate) {
		String str = null;
		long l = paramDate.getTime();
		if (isSameDay(l)) {
			Calendar localCalendar = GregorianCalendar
					.getInstance(Locale.CHINA);
			localCalendar.setTime(paramDate);
			int i = localCalendar.get(11);
			if (i > 17)
				str = StringUtils.getString(R.string.datecontent1) + " hh:mm";
			else if ((i >= 0) && (i <= 6))
				str = StringUtils.getString(R.string.datecontent2) + " hh:mm";
			else if ((i > 11) && (i <= 17))
				str = StringUtils.getString(R.string.datecontent3) + " hh:mm";
			else
				str = StringUtils.getString(R.string.datecontent4) + " hh:mm";
		} else if (isYesterday(l)) {
			str = StringUtils.getString(R.string.datecontent5) + " HH:mm";
		} else {
			str = "M月d日 HH:mm";
		}
		String time = new SimpleDateFormat(str, Locale.CHINA).format(paramDate);
		if (time.startsWith(StringUtils.getString(R.string.datecontent2)
				+ " 12")) {
			time = time.replace(StringUtils.getString(R.string.datecontent2)
					+ " 12", StringUtils.getString(R.string.datecontent2)
					+ " 00");
		}
		if (time.startsWith(StringUtils.getString(R.string.datecontent3) + " 0")) {
			time = time.replace(StringUtils.getString(R.string.datecontent3)
					+ " 0", StringUtils.getString(R.string.datecontent3) + " ");
		}
		if (time.startsWith(StringUtils.getString(R.string.datecontent1) + " 0")) {
			time = time.replace(StringUtils.getString(R.string.datecontent1)
					+ " 0", StringUtils.getString(R.string.datecontent1) + " ");
		}
		if (time.startsWith(StringUtils.getString(R.string.datecontent2) + " 0")) {
			time = time.replace(StringUtils.getString(R.string.datecontent2)
					+ " 0", StringUtils.getString(R.string.datecontent2) + " ");
		}
		if (time.startsWith(StringUtils.getString(R.string.datecontent4) + " 0")) {
			time = time.replace(StringUtils.getString(R.string.datecontent4)
					+ " 0", StringUtils.getString(R.string.datecontent4) + " ");
		}
		return time;
	}

	public static String getTimestampString2(Long time) {
		String str = null;
		long cur = System.currentTimeMillis();
		long tmp = (cur-time)/(60*1000);
		if(tmp<=60){
			if(tmp==0)tmp=1;
			str = tmp+"分钟前" ;
		}else if(tmp>60 && tmp<24*60){
			str = tmp/60+"小时前" ;
		}else{
			str = new SimpleDateFormat("M-d", Locale.CHINA).format(new Date(time));
		}
		return str;
	}
	public static String getTimestampStringBB(Long time) {
		String str = null;
		long cur = System.currentTimeMillis();
		long tmp = (cur-time)/(60*1000);
		if(tmp<=60){
			if(tmp==0)tmp=1;
			str = tmp+"分钟前" ;
		}else if(tmp>60 && tmp<24*60){
			str = tmp/60+"小时前" ;
		}else if(tmp>24*60 && tmp<24*60*2){
			str="1天前";
		}
		else if(tmp>24*60*2 && tmp<24*60*3){
			str="2天前";
		}else if(tmp>24*60*3 && tmp<24*60*4){
			str="3天前";
		}else if(tmp>24*60*4 && tmp<24*60*5){
			str="4天前";
		}else if(tmp>24*60*5 && tmp<24*60*6){
			str="5天前";
		}else if(tmp>24*60*6 && tmp<24*60*7){
			str="6天前";
		}else if(tmp>24*60*7 && tmp<24*60*8){
			str="7天前";
		}else if(!isSameYear(time,cur)){
			str = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(new Date(time));
		} else{
			str = new SimpleDateFormat("MM月dd日", Locale.CHINA).format(new Date(time));
		}
		return str;
	}

	private static boolean isSameYear(long date1, long date2){
		Calendar cal1 = Calendar.getInstance();
		cal1.setTimeInMillis(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTimeInMillis(date2);

		return  cal1.get(Calendar.YEAR) == cal2
				.get(Calendar.YEAR);
	}


	/**
	 * 获取现在时间
	 *
	 * @return返回字符串格式 MM月dd日 HH:mm
	 */
	public static String getStringDate(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日 HH:mm");
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 获取现在时间
	 *
	 * @return返回字符串格式 MM月dd日 HH:mm
	 */
	public static String getStringDate2(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("MM.dd");
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 获取现在时间
	 *
	 * @return返回字符串格式 MM月dd日 HH:mm
	 */
	public static String getStringDate3(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 获取现在时间
	 *
	 * @return返回字符串格式 MM月dd日 HH:mm
	 */
	public static String getStringDate4(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 获取现在时间
	 *
	 * @return返回字符串格式 MM月dd日 HH:mm
	 */
	public static String getStringDate5(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
		String dateString = formatter.format(date);
		return dateString;
	}

	public static boolean isCloseEnough(long paramLong1, long paramLong2) {
		long l = paramLong1 - paramLong2;
		if (l < 0L)
			l = -l;
		return (l < 30000L);
	}

	public static int getAge(String birth) {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.YEAR)
				- parseDate(birth).get(Calendar.YEAR);
	}

	private static Calendar parseDate(String s) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(format.parse(s));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return calendar;
	}

	public static boolean isSameDay(long paramLong) {
		TimeInfo localTimeInfo = getTodayStartAndEndTime();
		return ((paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo
				.getEndTime()));
	}

	private static boolean isYesterday(long paramLong) {
		TimeInfo localTimeInfo = getYesterdayStartAndEndTime();
		return ((paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo
				.getEndTime()));
	}

	public static Date StringToDate(String paramString1, String paramString2) {
		SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(
				paramString2);
		Date localDate = null;
		try {
			localDate = localSimpleDateFormat.parse(paramString1);
		} catch (ParseException localParseException) {
			localParseException.printStackTrace();
		}
		return localDate;
	}

	public static String toTime(int paramInt) {
		paramInt /= 1000;
		int i = paramInt / 60;
		int j = 0;
		if (i >= 60) {
			j = i / 60;
			i %= 60;
		}
		int k = paramInt % 60;
		return String.format("%02d:%02d", new Object[] { Integer.valueOf(i),
				Integer.valueOf(k) });
	}

	public static String toTimeBySecond(int paramInt) {
		int i = paramInt / 60;
		int j = 0;
		if (i >= 60) {
			j = i / 60;
			i %= 60;
		}
		int k = paramInt % 60;
		return String.format("%02d:%02d", new Object[] { Integer.valueOf(i),
				Integer.valueOf(k) });
	}

	public static TimeInfo getYesterdayStartAndEndTime() {
		Calendar localCalendar1 = Calendar.getInstance();
		localCalendar1.add(5, -1);
		localCalendar1.set(11, 0);
		localCalendar1.set(12, 0);
		localCalendar1.set(13, 0);
		localCalendar1.set(14, 0);
		Date localDate1 = localCalendar1.getTime();
		long l1 = localDate1.getTime();
		Calendar localCalendar2 = Calendar.getInstance();
		localCalendar2.add(5, -1);
		localCalendar2.set(11, 23);
		localCalendar2.set(12, 59);
		localCalendar2.set(13, 59);
		localCalendar2.set(14, 999);
		Date localDate2 = localCalendar2.getTime();
		long l2 = localDate2.getTime();
		TimeInfo localTimeInfo = new TimeInfo();
		localTimeInfo.setStartTime(l1);
		localTimeInfo.setEndTime(l2);
		return localTimeInfo;
	}

	public static TimeInfo getTodayStartAndEndTime() {
		Calendar localCalendar1 = Calendar.getInstance();
		localCalendar1.set(11, 0);
		localCalendar1.set(12, 0);
		localCalendar1.set(13, 0);
		localCalendar1.set(14, 0);
		Date localDate1 = localCalendar1.getTime();
		long l1 = localDate1.getTime();
		SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss S");
		Calendar localCalendar2 = Calendar.getInstance();
		localCalendar2.set(11, 23);
		localCalendar2.set(12, 59);
		localCalendar2.set(13, 59);
		localCalendar2.set(14, 999);
		Date localDate2 = localCalendar2.getTime();
		long l2 = localDate2.getTime();
		TimeInfo localTimeInfo = new TimeInfo();
		localTimeInfo.setStartTime(l1);
		localTimeInfo.setEndTime(l2);
		return localTimeInfo;
	}

	public static TimeInfo getBeforeYesterdayStartAndEndTime() {
		Calendar localCalendar1 = Calendar.getInstance();
		localCalendar1.add(5, -2);
		localCalendar1.set(11, 0);
		localCalendar1.set(12, 0);
		localCalendar1.set(13, 0);
		localCalendar1.set(14, 0);
		Date localDate1 = localCalendar1.getTime();
		long l1 = localDate1.getTime();
		Calendar localCalendar2 = Calendar.getInstance();
		localCalendar2.add(5, -2);
		localCalendar2.set(11, 23);
		localCalendar2.set(12, 59);
		localCalendar2.set(13, 59);
		localCalendar2.set(14, 999);
		Date localDate2 = localCalendar2.getTime();
		long l2 = localDate2.getTime();
		TimeInfo localTimeInfo = new TimeInfo();
		localTimeInfo.setStartTime(l1);
		localTimeInfo.setEndTime(l2);
		return localTimeInfo;
	}

	public static TimeInfo getCurrentMonthStartAndEndTime() {
		Calendar localCalendar1 = Calendar.getInstance();
		localCalendar1.set(5, 1);
		localCalendar1.set(11, 0);
		localCalendar1.set(12, 0);
		localCalendar1.set(13, 0);
		localCalendar1.set(14, 0);
		Date localDate1 = localCalendar1.getTime();
		long l1 = localDate1.getTime();
		Calendar localCalendar2 = Calendar.getInstance();
		Date localDate2 = localCalendar2.getTime();
		long l2 = localDate2.getTime();
		TimeInfo localTimeInfo = new TimeInfo();
		localTimeInfo.setStartTime(l1);
		localTimeInfo.setEndTime(l2);
		return localTimeInfo;
	}

	public static TimeInfo getLastMonthStartAndEndTime() {
		Calendar localCalendar1 = Calendar.getInstance();
		localCalendar1.add(2, -1);
		localCalendar1.set(5, 1);
		localCalendar1.set(11, 0);
		localCalendar1.set(12, 0);
		localCalendar1.set(13, 0);
		localCalendar1.set(14, 0);
		Date localDate1 = localCalendar1.getTime();
		long l1 = localDate1.getTime();
		Calendar localCalendar2 = Calendar.getInstance();
		localCalendar2.add(2, -1);
		localCalendar2.set(5, 1);
		localCalendar2.set(11, 23);
		localCalendar2.set(12, 59);
		localCalendar2.set(13, 59);
		localCalendar2.set(14, 999);
		localCalendar2.roll(5, -1);
		Date localDate2 = localCalendar2.getTime();
		long l2 = localDate2.getTime();
		TimeInfo localTimeInfo = new TimeInfo();
		localTimeInfo.setStartTime(l1);
		localTimeInfo.setEndTime(l2);
		return localTimeInfo;
	}

	public static String getTimestampStr() {
		return Long.toString(System.currentTimeMillis());
	}
}